package com.bluefox.uac.service;

import com.bluefox.uac.model.User;
import com.bluefox.uac.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @author SunFeng
 */
@Component
public class MyUserDetailsService implements UserDetailsService {


    @Autowired
    private UserRepository userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 从数据库获取该用户
        User user = userMapper.findByUsername(username);
        if(null==user){
            throw new UsernameNotFoundException("用户不存在");
        }
        //将字符串转换
        user.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRoles()));

        return user;
    }
}
